 package handling.channel.handler;
 import client.MapleCharacter;
import client.MapleClient;
import client.inventory.MapleInventoryType;
import constants.ServerConstants;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import scripting.map.NodeScriptManager;
import server.MapleInventoryManipulator;
import server.Timer;
import server.life.MapleMonster;
import server.life.MobAttack;
import server.life.MobSkill;
import server.life.MobSkillFactory;
import server.maps.AnimatedMapleMapObject;
import server.maps.MapleMap;
import server.maps.MapleMapObjectType;
import server.maps.MapleNodes;
import server.movement.LifeMovementFragment;
import tools.MaplePacketCreator;
import tools.Pair;
import tools.Randomizer;
import tools.data.input.LittleEndianAccessor;
import tools.data.input.SeekableLittleEndianAccessor;
import tools.packet.MobPacket;

import java.awt.*;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;






























 public class MobHandler {
/*  33 */   private static final Logger log = LogManager.getLogger(MobHandler.class);




















































































































































































































   public static void MoveMonster(SeekableLittleEndianAccessor slea, MapleClient c, MapleCharacter chr) {
/* 247 */     if (chr == null || chr.getMap() == null) {
       return;
     }
/* 250 */     int objectid = slea.readInt();

/* 252 */     MapleMonster monster = chr.getMap().getMonsterByOid(objectid);

/* 254 */     if (monster == null || monster.getType() != MapleMapObjectType.MONSTER) {
       return;
     }

/* 258 */     short moveSN = slea.readShort();

/* 260 */     byte bOption = slea.readByte();
/* 261 */     byte actionAndDir = slea.readByte();
/* 262 */     int skillId = slea.readShort();
/* 263 */     int skillLevel = slea.readShort();
/* 264 */     boolean changeController = ((bOption & 0x10) == 16);
/* 265 */     boolean movingAttack = ((bOption & 0x1) == 1);
/* 266 */     short effectAfter = slea.readShort();
/* 267 */     short unk = slea.readShort();
/* 268 */     boolean usedSkill = false;
/* 269 */     int realSkillID = 0;
/* 270 */     int realSkillLevel = 0;
/* 271 */     boolean usedAttkact = false;
/* 272 */     int forceAttackIdx = 0;
/* 273 */     int action = actionAndDir;

/* 275 */     if (action < 0) {
/* 276 */       action = -1;
     } else {
/* 278 */       action >>= 1;
     }




/* 284 */     if (action >= 12 && action <= 29) {












/* 297 */       int attackIdx = monster.getStats().getAttacks().size();

/* 299 */       MobAttack attack = monster.getStats().getAttacks().get(Randomizer.nextInt(attackIdx));
/* 300 */       forceAttackIdx = attack.getAction();
/* 301 */       if (attack.getAfterAttack() != -1) {
/* 302 */         monster.getMap().broadcastMessage(MobPacket.OnMobSetAfterAttack(objectid, attack.getAfterAttack(), action, ((actionAndDir & 0x1) != 0)));
       }
/* 304 */       usedAttkact = true;
/* 305 */     } else if (action >= 30 && action <= 46) {

/* 307 */       usedSkill = true;
/* 308 */       if (monster.getNextSkill() == skillId && monster.getNextSkillLvl() == skillLevel && monster.hasSkill(skillId, skillLevel)) {
/* 309 */         MobSkill msi = MobSkillFactory.getMobSkill(skillId, skillLevel);
/* 310 */         if (msi != null) {
/* 311 */           if (monster.getStats().getSkillAfter(skillId, skillLevel) != null) {
/* 312 */             msi.setMobSkillDelay(c.getPlayer(), monster, monster.getStats().getSkillAfter(skillId, skillLevel).intValue(), (short)0);
/* 313 */             if (ServerConstants.isShowPacket()) {
/* 314 */               Date dd = new Date();
/* 315 */               SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/* 316 */               String time = sim.format(dd);
/* 317 */               System.err.println(time + " 怪物延迟技能:" + skillId + " /" + skillLevel);
             }
/* 319 */           } else if (msi.getSkillAfter() > 0) {
/* 320 */             msi.setMobSkillDelay(c.getPlayer(), monster, msi.getSkillAfter(), (short)0);
/* 321 */             if (ServerConstants.isShowPacket()) {
/* 322 */               System.err.println("SkillAfter:" + msi.getSkillAfter() + " Info:" + skillId + " /" + skillLevel);
             }
           } else {

/* 326 */             Timer.MapTimer.getInstance().schedule(() -> { if (monster.isAlive()) msi.applyEffect(chr, monster, effectAfter, true);  },550L);





/* 332 */             long cooltime = msi.getCoolTime();
/* 333 */             switch (skillId) {
               case 146:
/* 335 */                 if (skillLevel == 14) {
/* 336 */                   cooltime = msi.getDuration();
                 }
                 break;

               case 252:
/* 341 */                 cooltime = msi.getDuration();
                 break;
             }

/* 345 */             if (ServerConstants.isShowPacket()) {
/* 346 */               Date dd = new Date();
/* 347 */               SimpleDateFormat sim = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
/* 348 */               String time = sim.format(dd);
/* 349 */               System.err.println(time + " 怪物:" + monster.getId() + " 技能:" + skillId + "(" + skillLevel + ") 冷却时间:" + cooltime);
             }

/* 352 */             monster.setLastSkillUsed(skillId, skillLevel, System.currentTimeMillis(), cooltime);

/* 354 */             monster.setNextSkill(0);
/* 355 */             monster.setNextSkillLvl(0);
/* 356 */             usedSkill = false;
           }
         }
       }
     }


/* 363 */     if (!usedSkill && monster.getSkills().size() > 0) {
/* 364 */       if (monster.getNextSkill() == 0) {

/* 366 */         Pair<Integer, Integer> skillToUse = monster.getSkills().get((byte)Randomizer.nextInt(monster.getSkills().size()));





/* 372 */         int nextSkillID = ((Integer)skillToUse.getLeft()).intValue();

/* 374 */         int nextSkillLevel = ((Integer)skillToUse.getRight()).intValue();


/* 377 */         MobSkill mobSkill = MobSkillFactory.getMobSkill(nextSkillID, nextSkillLevel);

/* 379 */         if (mobSkill != null && !mobSkill.checkCurrentBuff(chr, monster)) {

/* 381 */           long now = System.currentTimeMillis();

/* 383 */           long ls = monster.getLastSkillUsed(nextSkillID, nextSkillLevel);

/* 385 */           if (ls == 0L || (now - ls > mobSkill.getCoolTime() && !mobSkill.onlyOnce())) {

/* 387 */             monster.setNextSkill(nextSkillID);

/* 389 */             monster.setNextSkillLvl(nextSkillLevel);

/* 391 */             int reqHp = (int)((float)monster.getHp() / (float)monster.getMobMaxHp() * 100.0F);
/* 392 */             if (reqHp <= mobSkill.getHP()) {
/* 393 */               mobSkill.SkillMessage(monster);
             }
           }
         }
       } else {

/* 399 */         realSkillID = monster.getNextSkill();

/* 401 */         realSkillLevel = monster.getNextSkillLvl();
       }
     }







































/* 443 */     if (usedAttkact) {
/* 444 */       realSkillID = 0;
/* 445 */       realSkillLevel = 0;
     }

/* 448 */     if (monster.getController() != null && monster.getController().getId() != c.getPlayer().getId()) {
/* 449 */       if (!changeController) {
/* 450 */         c.announce(MobPacket.stopControllingMonster(objectid));
         return;
       }
/* 453 */       monster.switchController(chr, true);
     }


/* 457 */     int count = slea.readByte();
/* 458 */     List<Pair<Short, Short>> multiTargetForBall = new ArrayList<>();
/* 459 */     for (int i = 0; i < count; i++) {
/* 460 */       multiTargetForBall.add(new Pair(Short.valueOf(slea.readShort()), Short.valueOf(slea.readShort())));
     }

/* 463 */     int unkV170 = slea.readInt();
/* 464 */     count = slea.readByte();
/* 465 */     List<Short> randTimeForAreaAttack = new ArrayList<>();
/* 466 */     for (int j = 0; j < count; j++) {
/* 467 */       randTimeForAreaAttack.add(Short.valueOf(slea.readShort()));
     }

/* 470 */     count = slea.readByte();





/* 476 */     List<Integer> unkList = new ArrayList<>();
/* 477 */     for (int k = 0; k < count; k++) {
/* 478 */       unkList.add(Integer.valueOf(slea.readInt()));
     }

/* 481 */     slea.readByte();

/* 483 */     slea.readInt();
/* 484 */     slea.readInt();
/* 485 */     slea.readInt();
/* 486 */     slea.readInt();
/* 487 */     slea.readByte();

/* 489 */     int v169 = slea.readInt();

/* 491 */     int tEncodedGatherDuration = slea.readInt();
/* 492 */     Point startPos = slea.readPos();
/* 493 */     Point velPos = slea.readPos();

/* 495 */     List<LifeMovementFragment> res = MovementParse.parseMovement((LittleEndianAccessor)slea, 2);

/* 497 */     if (c.getPlayer() != null)
     {
/* 499 */       c.announce(MobPacket.moveMonsterResponse(objectid, moveSN, monster.getMp(), movingAttack, realSkillID, realSkillLevel, forceAttackIdx));
     }

/* 502 */     if (res != null && res.size() > 0) {
/* 503 */       startPos = monster.getPosition();
/* 504 */       MapleMap map = chr.getMap();
/* 505 */       Point endPos = monster.getTruePosition();

/* 507 */       MovementParse.updatePosition(res, (AnimatedMapleMapObject)monster, -1);

/* 509 */       map.moveMonster(monster, endPos);


/* 512 */       map.broadcastMessage(chr, MobPacket.moveMonster(bOption, actionAndDir, realSkillID, realSkillLevel, objectid, v169, tEncodedGatherDuration, effectAfter, unk, unkV170, startPos, velPos, res, multiTargetForBall, randTimeForAreaAttack, unkList), endPos);
     }
   }


   public static void MobSkillDelayEnd(LittleEndianAccessor slea, MapleCharacter chr) {
/* 518 */     MapleMonster monster = chr.getMap().getMonsterByOid(slea.readInt());
/* 519 */     if (monster != null) {
/* 520 */       int skillID = slea.readInt();
/* 521 */       int skillLv = slea.readInt();
/* 522 */       short remainCount = 0;
/* 523 */       if (slea.readByte() == 1) {
/* 524 */         remainCount = (short)slea.readInt();
       }


/* 528 */       if (skillID == monster.getNextSkill() && skillLv == monster.getNextSkillLvl()) {
/* 529 */         MobSkill msi = MobSkillFactory.getMobSkill(skillID, skillLv);
/* 530 */         long cooltime = msi.getCoolTime();


/* 533 */         monster.setLastSkillUsed(skillID, skillLv, System.currentTimeMillis(), cooltime);


/* 536 */         msi.applyEffect(chr, monster, 0, true);
/* 537 */         monster.setNextSkill(0);
/* 538 */         monster.setNextSkillLvl(0);
       }
     }
   }





   public static void FriendlyDamage(LittleEndianAccessor slea, MapleCharacter chr) {
/* 548 */     MapleMap map = chr.getMap();
/* 549 */     if (map == null) {
       return;
     }
/* 552 */     MapleMonster mobfrom = map.getMonsterByOid(slea.readInt());
/* 553 */     slea.skip(4);
/* 554 */     MapleMonster mobto = map.getMonsterByOid(slea.readInt());

/* 556 */     if (mobfrom != null && mobto != null && mobto.getStats().isFriendly()) {
/* 557 */       int damage = mobto.getStats().getLevel() * Randomizer.nextInt(mobto.getStats().getLevel()) / 2;
/* 558 */       mobto.damage(chr, damage, true);
/* 559 */       checkShammos(chr, mobto, map);
     }
   }

   public static void checkShammos(MapleCharacter chr, MapleMonster mobto, MapleMap map) {
/* 564 */     if (!mobto.isAlive() && mobto.getStats().isEscort()) {
/* 565 */       for (MapleCharacter chrz : map.getCharactersThreadsafe()) {
/* 566 */         if (chrz.getParty() != null && chrz.getParty().getLeader().getId() == chrz.getId()) {

/* 568 */           if (chrz.haveItem(2022698)) {
/* 569 */             MapleInventoryManipulator.removeById(chrz.getClient(), MapleInventoryType.USE, 2022698, 1, false, true);
/* 570 */             mobto.heal((int)mobto.getMobMaxHp(), mobto.getMobMaxMp(), true);
             return;
           }
           break;
         }
       }
/* 576 */       map.broadcastMessage(MaplePacketCreator.serverNotice(6, "Your party has failed to protect the monster."));
/* 577 */       MapleMap mapp = chr.getMap().getForcedReturnMap();
/* 578 */       for (MapleCharacter chrz : map.getCharactersThreadsafe()) {
/* 579 */         chrz.changeMap(mapp, mapp.getPortal(0));
       }
/* 581 */     } else if (mobto.getStats().isEscort() && mobto.getEventInstance() != null) {
/* 582 */       mobto.getEventInstance().setProperty("HP", String.valueOf(mobto.getHp()));
     }
   }






   public static void MonsterBomb(int oid, MapleCharacter chr) {
/* 592 */     MapleMonster monster = chr.getMap().getMonsterByOid(oid);
/* 593 */     if (monster == null || !chr.isAlive() || chr.isHidden() || monster.getLinkCID() > 0) {
       return;
     }
/* 596 */     byte selfd = monster.getStats().getSelfD();
/* 597 */     if (selfd != -1) {
/* 598 */       monster.setHp(0L);
/* 599 */       chr.getMap().killMonster(monster, chr, false, false, selfd);
     }
   }





   public static void AutoAggro(int monsteroid, MapleCharacter chr) {
/* 608 */     if (chr == null || chr.getMap() == null || chr.isHidden()) {
       return;
     }
/* 611 */     MapleMonster monster = chr.getMap().getMonsterByOid(monsteroid);
/* 612 */     if (monster != null && monster.getLinkCID() <= 0) {
/* 613 */       if (monster.getController() != null) {
/* 614 */         if (chr.getMap().getCharacterById(monster.getController().getId()) == null) {
/* 615 */           monster.switchController(chr, true);
         } else {
/* 617 */           monster.switchController(monster.getController(), true);
         }
       } else {
/* 620 */         monster.switchController(chr, true);
       }
     }
   }


   public static void MonsterSpecialSkill(LittleEndianAccessor slea, MapleCharacter chr) {
/* 627 */     if (chr == null) {
       return;
     }

/* 631 */     int moboid = slea.readInt();
/* 632 */     int unk_1 = slea.readInt();
/* 633 */     int unk_2 = slea.readInt();
/* 634 */     int unk_3 = slea.readInt();
/* 635 */     int unk_4 = slea.readInt();
   }



   public static void MobTimeBombEnd(LittleEndianAccessor lea, MapleCharacter player) {
/* 641 */     MapleMonster monster = player.getMap().getMonsterByOid(lea.readInt());
/* 642 */     if (monster != null) {
/* 643 */       player.getClient().announce(MobPacket.mobEscortReturnBefore(monster, player.getMap()));
     }
   }

   public static void MobEscortCollision(LittleEndianAccessor lea, MapleCharacter player) {
/* 648 */     MapleMonster monster = player.getMap().getMonsterByOid(lea.readInt());
/* 649 */     int n2 = lea.readInt();
/* 650 */     int n3 = player.getMap().getNodes().size();
/* 651 */     if (monster != null && n3 > 0) {
/* 652 */       MapleNodes.MapleNodeInfo node = player.getMap().getNode(n2);
/* 653 */       if (node == null) {
         return;
       }
/* 656 */       if (node.attr == 2) {
/* 657 */         switch (player.getMapId() / 100) {
           case 9211200:
           case 9211201:
           case 9211202:
           case 9211203:
           case 9211204:
/* 663 */             if (node.stopInfos == null || node.stopInfos.sayInfo.size() <= 0)
/* 664 */               break;  player.getMap().talkMonster(node.stopInfos, 5120035, monster.getObjectId());
             break;

           case 9320001:
           case 9320002:
           case 9320003:
/* 670 */             if (node.stopInfos == null || node.stopInfos.sayInfo.size() <= 0)
/* 671 */               break;  player.getMap().talkMonster(node.stopInfos, 5120051, monster.getObjectId());
             break;

           case 9230402:
/* 675 */             if (node.stopInfos == null || node.stopInfos.sayInfo.size() <= 0)
/* 676 */               break;  player.getMap().talkMonster(node.stopInfos, 5120052, monster.getObjectId());
             break;
         }
/* 679 */         if (!node.stopInfos.scriptName.isEmpty()) {
/* 680 */           NodeScriptManager.getInstance().getNodeScript(player.getClient(), node.stopInfos.scriptName, node.stopInfos.stopDuration, monster.getObjectId());
         }
/* 682 */         if (node.stopInfos.sayInfo.size() > 0) {
/* 683 */           Timer.MapTimer.getInstance().schedule(() -> player.send(MobPacket.MobEscortStopEndPermmision(monster.getObjectId())), node.stopInfos.stopDuration);
         }
       }



/* 689 */       monster.setLastNode(n2);
/* 690 */       if (player.getMap().isLastNode(n2))
/* 691 */         switch (player.getMapId() / 100) {
           case 9211200:
           case 9211201:
           case 9211202:
           case 9211203:
           case 9211204:
           case 9320001:
           case 9320002:
           case 9320003:
/* 700 */             player.send_other(MaplePacketCreator.showEffect("aswan/clear"), true);
/* 701 */             player.send_other(MaplePacketCreator.playSound("Party1/Clear"), true);
/* 702 */             player.getMap().broadcastMessage(MaplePacketCreator.serverEvent("进入下一个阶段。"));
/* 703 */             player.getEventInstance().setProperty("checkdone", String.valueOf(player.getMapId()));
             break;

           case 9230401:
           case 9230402:
/* 708 */             player.send_other(MaplePacketCreator.showEffect("aswan/clear"), true);
/* 709 */             player.send_other(MaplePacketCreator.playSound("Party1/Clear"), true);
/* 710 */             player.getEventInstance().getEventManager().setProperty("state", "" + (player.getMapId() / 100 % 10 + 1) + "");
             break;
         }
     }
   }
 }


/* Location:              C:\PGX\Server.jar!\handling\channel\handler\MobHandler.class
 * Java compiler version: 8 (52.0)
 * JD-Core Version:       1.1.3
 */